package com.cheyun.netsalev3.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.RadioAdapter2
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.widget.RadioData
import com.cheyun.netsalev3.databinding.RadioGroupViewBinding
import com.cheyun.netsalev3.viewmodel.AddClueActivityViewModel
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.cheyun.netsalev3.viewmodel.ClueFollowUpActivityViewModel
import com.cheyun.netsalev3.viewmodel.CustomerTagActivityViewModel
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout

/**
 * 单选组件
 */
class RadioGroupView : LinearLayout {
    var activity: AppCompatActivity? = null

    var binding: RadioGroupViewBinding? = null

    var mContext: Context? = null

    var viewModel: BaseViewModel? = null

    //当前组件的索引
    var pos: Any = 0
    //是否锁定使用
    var lock: Boolean = false
    //初始值
    var initValue = ""
    //最大显示行数
    var maxLine: Int = 1
    //来源
    var from = ""
    //数据源
    var customerDatas = ArrayList<RadioData>()

    var maxCount = 0

    //new
    var adapter: RadioAdapter2? = null

    init {
        activity = context as AppCompatActivity
    }

    var dd = ObservableField<ArrayList<DialogParam>>()

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.mContext = context
        binding = RadioGroupViewBinding.inflate(LayoutInflater.from(getContext()), this, true)
        val a = context.obtainStyledAttributes(attrs, R.styleable.radioGroupView)
        var column = a.getInt(R.styleable.radioGroupView_radioColumn, 3)
        maxCount = a.getInt(R.styleable.radioGroupView_radioMaxCount, 0)
        maxLine = a.getInt(R.styleable.radioGroupView_radioLine, 1)
        lock = a.getBoolean(R.styleable.radioGroupView_radioLock, false)
        if (a.hasValue(R.styleable.radioGroupView_radioInitValue)) {
            initValue = a.getString(R.styleable.radioGroupView_radioInitValue)
        }
        if (a.hasValue(R.styleable.radioGroupView_radioFrom)) {
            from = a.getString(R.styleable.radioGroupView_radioFrom)
        }
        setColumn(column)
        initView()
    }

    private fun initView() {
    }

    fun setLine(line: Int) {
        maxLine = line
    }
    fun setMaxCount1(count: Int) {
        if(count>0){
            maxCount = count
        }
//        if(maxCount == 0) {
//            maxCount = count
//        }
    }
    fun setDefaultValue(value: String) {
        initValue = value
    }

    fun setColumn(column: Int) {
//        binding?.glView?.setNumColumns(column)
//        binding?.glView?.setNumRows(maxLine)
        if(maxCount == 0) {
            maxCount = column * maxLine
        }
//        binding?.glView?.setAdapter(RadioAdapter(this.context))

        adapter = RadioAdapter2(mContext, customerDatas)
        binding?.idFlowlayout?.setAdapter(adapter)
    }

    fun setPosition(pos: Any) {
        this.pos = pos
    }

    fun bindViewModel(viewModel: BaseViewModel) {
        this.viewModel = viewModel
    }

    fun lockClick(lock: Boolean) {
        this.lock = lock
    }

    fun setData(data: java.util.ArrayList<DialogParam>) {
        if(data == null || data.size == 0){
            return
        }

        customerDatas.clear()
        for (index in 0 until data.size) {
            var d = data.get(index)
            var selected = d.IsSelect
            if (initValue.isNotEmpty()) {
                selected = initValue.equals(d.id)
            }
            customerDatas.add(RadioData(selected, d.name, d.id))
        }
        setCheckedPos()

        if(maxCount == 0) {
            adapter?.setData(customerDatas)
        }else {
            if(maxCount>=customerDatas.size){
                adapter?.setData(customerDatas)
            }else{
                adapter?.setData(customerDatas.subList(0,maxCount))
            }
        }
        if(!lock){
            binding?.idFlowlayout?.setOnTagClickListener(object : TagFlowLayout.OnTagClickListener{
                override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
                    customerDatas.forEach { c -> c.checked = false }
                    customerDatas.get(position).checked = true
                    adapter?.notifyDataChanged()
                    if(viewModel is CustomerTagActivityViewModel){
                        (viewModel as CustomerTagActivityViewModel).radioGroupCallback(pos, data.get(position), from)
                    }
//                    if (viewModel is ClueFollowUpActivityViewModel) {
//                        (viewModel as ClueFollowUpActivityViewModel).radioGroupCallback(pos, data.get(position), from)
//                    } else if (viewModel is AddClueActivityViewModel) {
//                        (viewModel as AddClueActivityViewModel).radioGroupCallback(pos, data.get(position))
//                    }
                    return true
                }
            })
        }
    }

    fun refeshSourceData(data: java.util.ArrayList<DialogParam>) {
        customerDatas.clear()
        for (index in 0 until data.size) {
            var d = data.get(index)
            customerDatas.add(RadioData(d.IsSelect, d.name, d.id))
        }
        setCheckedPos()
//        binding?.glView?.getAdapter()?.replaceAll(customerDatas)
        if(maxCount == 0) {
            adapter?.setData(customerDatas)
        }else {
            if(maxCount>=customerDatas.size){
                adapter?.setData(customerDatas)
            }else{
                adapter?.setData(customerDatas.subList(0,maxCount))
            }
        }
    }

    //修改默认选中项位置　１.当前页能够显示的项有默认项，顺序不变　2,默认项不是当前能显示的项，重新排序，默认项在第一位
    fun setCheckedPos() {
        var checkedPos = -1
        for (index in 0 until customerDatas.size) {
            if (customerDatas.get(index).checked) {
                checkedPos = index
            }
        }
        var defaultAtPos1 = false
        if (checkedPos > 0 && checkedPos >= maxCount) {
            defaultAtPos1 = true
        }
        if (defaultAtPos1) {
            customerDatas.sortByDescending { it.checked }
        }
    }
}